//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "client\fn_clientSetupPickup.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_item_desc","_action_title","_pickup_msg","_shared_variable","_shared_variable_value","_pickup_code","_action_id","_is_picked_up","_action_display_timer"];
DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);
DEFAULT_PARAM(_action,TMF_REGISTER_ACTION,[]);
DEFAULT_PARAM(_instance_id,TMF_REGISTER_INSTANCE,TMF_SERVER_INSTANCE_ID);

if (!isnull _object) then {
	_action_id = [_action] call FFUNC(actionGetID);
	TRACE_1("Action",_action);
	_parameters = [_action] call FFUNC(actionGetParams);

	_item_desc = [_parameters, "ITEM_DESC", ([_object] call CFUNC(displayName))] call CFUNC(hashGet);
	_pickup_msg = [_parameters, "PICKUP_MSG", []] call CFUNC(hashGet);
	_shared_variable = [_parameters, "SHARED_VAR", ""] call CFUNC(hashGet);
	_initial_status = false; //[_parameters, "SHARED_VAR_VAL", false] call CFUNC(hashGet);
	_action_title = [_parameters, "PICKUP_TITLE", "STR_MF_PICKUP_DEFACTION"] call CFUNC(hashGet);
	_pickup_code = [_parameters, "PICKUP_CODE", ""] call CFUNC(hashGet);

	_object allowDamage false;

	_obj_var_picked = format["mission_object_pickedup_%1",_action_id];
	_is_picked_up = OGETVAR(_object,_obj_var_picked,_initial_status);
	
	If (isNil "_is_picked_up") then {
		_is_picked_up = _initial_status;
	};
	
	_act_id = [_object,_action,"PICKUP_ACTION",-1] call FFUNC(actionGetLocal);
	if (_act_id >= 0) then {
		TRACE_1("Remove Action",_act_id);
		_object removeAction _act_id;
	};
	
	if (_is_picked_up) then {
		TRACE_1("Object is picked up");
		_object hideObject true;
	};
	if (_action_title != "") then {
		_action_title = (_action_title) call CFUNC(translate);
		_arguments = [];
		_arguments set [TMF_EXECUTE_ACTION_PARAM_ACTID, _action_id];
		_action_display_timer = QGVAR(action_display_timer);
		_object setVariable [_action_display_timer,0];
		_condition = "";
		if (_initial_status) then {
			_condition = Format["(alive _target) && (_target getVariable '%1') && (_target getVariable '%2' < time)",_obj_var_picked,_action_display_timer];
		} else {
			_condition = Format["(alive _target) && !(_target getVariable '%1') && (_target getVariable '%2' < time)",_obj_var_picked,_action_display_timer];
		};
		_act_id = _object addAction [_action_title, TMF_ADD_ACTION_SCRIPT,_arguments,10,True,True,"",_condition];
		// We set the variable as local because it is dependent on the computer running it.
		[_object,_action,"PICKUP_ACTION",_act_id] call FFUNC(actionSetLocal);
		TRACE_1("Added Action",ARR_4(_act_id,_action_title,typeof _object,_condition));
	};
};

TRACE("End");
TMFPROFILERSTOP;